﻿/* =========================================
 * Description: Create Account v1
 * 
 * Functions:
 * -Account creator for admin/employees
 * 
 * -----------------------------------------
 * Date Finished: February 16, 2010
 * Author: Nikko Fernando O. Santillan
 * =========================================
 * 
 * Description: Create Account v1.1
 * 
 * Updates:
 * -create account linked to login
 * 
  -----------------------------------------
 * Date Finished: February 21, 2010
 * Author: Nikko Fernando O. Santillan
 * =========================================
 * */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace sahcpims
{

    public partial class createAccount : Form
    {
       

        public createAccount()
        {
            InitializeComponent();
               
        }

        public int doFlag = 0;
      

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click()
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
          
       
            
        }

        private void signUp_Click(object sender, EventArgs e)
        {
            logIn login = new logIn();

            this.Visible = false;
            login.Visible = true;
           
        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            logIn login = new logIn();

            //this string will be the query string for the connection to the database
            string accountString =
                @"Provider=Microsoft.Jet.OLEDB.4.0;"
                +@"Data source=..\\accounts.mdb";

           
            OleDbConnection account = new OleDbConnection(accountString);
            DataSet detaSet = new DataSet();

            //opens account database
            account.Open();

            if (!passtextBox.Text.ToString().Equals(verifyPassTextBox.Text.ToString()))
            {
                verificationMessageLabel.Text = "Message: Input Password and Verification not the same. Try Again.";
                passtextBox.ResetText();
                verifyPassTextBox.ResetText();
            }
            else
            {
              
                //for connection to the database
                try
                {
                    OleDbDataAdapter tagalagay = new OleDbDataAdapter("INSERT into accounts "
                                              + "values('" + usernametextBox.Text.ToString() + "','" + passtextBox.Text.ToString() + "','" + int.Parse(employeeNumberTextBox.Text) + "','" + nameTextBox.Text.ToString() + "')", account);

                    tagalagay.Fill(detaSet);
                    doFlag = 0;

                }
                //catches exception if the user input is invalid
                catch (FormatException)
                {
                    
                    verificationMessageLabel.Text = "Message: Invalid Input. Try Again.";
                    doFlag = 1;

                }
                //catches exception if the input is already in the database or the input is not saved in the database
                catch (OleDbException)
                {
                    verificationMessageLabel.Text = "Message: Data not Saved in Database. Data Already Exists.";
                    doFlag = 1;

                }
                
                //closes accounts database
                account.Close();

                //if registration is successful, the form is disabled
                if (doFlag == 0)
                {

                    verificationMessageLabel.ResetText();
                    userNameForm.Text = "Username: " + usernametextBox.Text;
                    PasswordForm.Text = "Employee: " + nameTextBox.Text;
                    verifyPassword.Text = "Employee Number: " + employeeNumberTextBox.Text;
                    
                    usernametextBox.ResetText();
                    passtextBox.ResetText();
                    verifyPassTextBox.ResetText();
                    employeeNumberTextBox.ResetText();
                    nameTextBox.ResetText();

                    signUpLabel.Visible = false;
                    nameLabel.Visible = false;
                    empNumLabel.Visible = false;

                    usernametextBox.Visible = false;
                    passtextBox.Visible = false;
                    employeeNumberTextBox.Visible = false;
                    nameTextBox.Visible = false;
                    registerButton.Visible = false;
                    verifyPassTextBox.Visible = false;

                   
                    regSuccessLabel.Visible = true;
                    regSuccessLabel.Text = "Registration Successful!";
                    registerButton.Visible = false;

                    backButton.Visible = true;
                  
                }
            }
           
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            logIn back = new logIn();
            this.Visible = false;
            back.Visible = true;
        }

        
 
    }
       
}
